﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using DDOS.Game_State;
using System.IO;
using Microsoft.Xna.Framework.Storage;

namespace DDOS
{
    class MainMenuLayer: ScreenLayer
    {
        private Color selected;
        private Color deselected;
        private Color[] selector;
        private int pnt;
        private String leftText;
        private Char[] bottomTextArray;
        private String bottomText;
        private int bottompnt;
        private Char[] rightTextArray;
        private String rightText;
        private int rightpnt;
        private int leftpnt;
        private int timer;
        private Char[] leftTextArray;

        public MainMenuLayer()
            : base(false)
        {
            timer = 0;
            selector = new Color[3];
            pnt = 0;
            deselected = Color.White;
            selected = Color.Red;
            selector[0] = selected;
            selector[1] = Color.White;
            selector[2] = Color.White;
            bottomTextArray = ReadFile("Game State/Level0");
            bottomText = bottomTextArray[0].ToString();
            rightTextArray = ReadFile("Game State/MainMenuLayer");
            rightText = rightTextArray[0].ToString();
            
            bottompnt = 0;
            rightpnt = 0;
            leftpnt = 0;
            string temp = "Please Make a Selection_";
            leftTextArray = temp.ToCharArray();
            leftText = leftTextArray[0].ToString();
            SoundManager.Instance.Play(SoundManager.Instance.MenuSang, true);
        }

        public Char[] ReadFile(String file)
        {
            String data = string.Empty;
            StreamReader fileStream;
            string thisLine = string.Empty;

#if XBOX360
            fileStream = File.OpenText(StorageContainer.TitleLocation + file +".cs");
#else
            fileStream = File.OpenText("../../../" +file + ".cs");
#endif

            while ((thisLine = fileStream.ReadLine()) != null)
            {
                data += thisLine;
            }
            
            fileStream.Dispose();
            return data.ToCharArray();
        }
        
        public override void Update(Microsoft.Xna.Framework.GameTime gt)
        {
            timer += 1;
            if (timer > 2600)
                timer = 0;
            if (timer % 2 == 0)
            {
                bottompnt += 1;
                rightpnt += 1;
                bottomText += bottomTextArray[bottompnt];
                rightText += rightTextArray[rightpnt];
            }
            if (timer % 5 == 0 && leftpnt < 22)
            {
                leftpnt += 1;
                leftText += leftTextArray[leftpnt].ToString();
            }
            else
            {
                if (timer % 20 == 0)
                {
                    if (leftText == "Please Make a Selection")
                        leftText = "Please Make a Selection_";
                    else
                        leftText = "Please Make a Selection";
                }
            }
            if (timer % 290 == 0)
                bottomText += "\r\n";
            if (timer % 105 == 0)
                rightText += "\r\n";
            if (timer % 2600 == 0)
            {
                bottompnt = 0;
                bottomText = "";
            }
            if (timer % 2000 == 0)
            {
                rightpnt = 0;
                rightText = "";
            }

            if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Down)||InputManager.InputManagerInstance.GamePadThumbstickMovedDown(InputManager.GamePadThumbstick.left) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadDown))
            {
                selector[pnt] = deselected;
                pnt +=1;
                if (pnt > 2)
                    pnt = 2;
                selector[pnt] = selected;
            }

            if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Up) || InputManager.InputManagerInstance.GamePadThumbstickMovedUp(InputManager.GamePadThumbstick.left) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadUp))
            {
                selector[pnt] = deselected;
                pnt -= 1;

                if (pnt < 0)
                    pnt = 0;
                selector[pnt] = selected;
            }

            if (selector[2] == selected && (InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.A) || InputManager.InputManagerInstance.KeyWasReleased(Keys.Enter)))
            {   
                ScreenManager.Instance.game.Exit();
            }
            if (selector[0] == selected && (InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.A) || InputManager.InputManagerInstance.KeyWasReleased(Keys.Enter)))
            {
                TerminalLayer tlayer = new TerminalLayer();
                TerminalScreen tScreen = new TerminalScreen();
                tScreen.PushLayer(tlayer);
                ScreenManager.Instance.LoadScreen(tScreen);
            }

            if (selector[1] == selected && InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.A) || InputManager.InputManagerInstance.KeyWasReleased(Keys.Enter))
            {
            }
        }

        public override void Draw()
        {
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.MainMenu, Vector2.Zero, Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times40, "New Game", new Vector2(ScreenManager.Instance.ScreenBounds.Center.X -65 -(0.5f * FontManager.Instance.Kootenay48.MeasureString("New Game").X), ScreenManager.Instance.ScreenBounds.Center.Y-120), selector[0]);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times40, "Load Game", new Vector2(ScreenManager.Instance.ScreenBounds.Center.X - 70 - (0.5f * FontManager.Instance.Kootenay48.MeasureString("Load Game").X), ScreenManager.Instance.ScreenBounds.Center.Y - 40), selector[1]);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times40, "Exit", new Vector2(ScreenManager.Instance.ScreenBounds.Center.X - 90 - (0.5f * FontManager.Instance.Kootenay48.MeasureString("Exit").X), ScreenManager.Instance.ScreenBounds.Center.Y + 40), selector[2]);
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.BottomConsole, new Vector2(250, (float)(ScreenManager.Instance.ScreenBounds.Bottom - ImageManager2D.Instance.BottomConsole.Height)), Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.LeftConsole, new Vector2(20, (float)(ScreenManager.Instance.ScreenBounds.Top + 10)), Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.RightConsole, new Vector2(ScreenManager.Instance.ScreenBounds.Right- 420, (float)(ScreenManager.Instance.ScreenBounds.Top + 10)), Color.White); 
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times12, bottomText, new Vector2(260, (float)(ScreenManager.Instance.ScreenBounds.Bottom - ImageManager2D.Instance.BottomConsole.Height +5)), Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times12, rightText, new Vector2(870, (float)(ScreenManager.Instance.ScreenBounds.Top + 20)), Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.DrawString(FontManager.Instance.Times12, leftText, new Vector2(23, (float)(ScreenManager.Instance.ScreenBounds.Top + 300)), Color.White);
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.DdosLogo, new Vector2(370, 10), Color.White);
        }
    }
}
